package tongcheng.com.tongcheng.ui.yemian.login.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.yanzhenjie.nohttp.RequestMethod;

import butterknife.Bind;
import tongcheng.com.tongcheng.ConStans;
import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.base.BaseActivity;
import tongcheng.com.tongcheng.bean.StatusBean;
import tongcheng.com.tongcheng.network.CallServer;
import tongcheng.com.tongcheng.network.MyRequest;
import tongcheng.com.tongcheng.network.OnCallBack;
import tongcheng.com.tongcheng.utils.GsonUtil;
import tongcheng.com.tongcheng.utils.ShowToast;
import tongcheng.com.tongcheng.utils.TimeCount;
import tongcheng.com.tongcheng.utils.ToastUtil;

public class ForGetPassWordActivity extends BaseActivity {


    @Bind(R.id.forget_back)
    ImageView mForgetBack;
    @Bind(R.id.forget_icon)
    ImageView mForgetIcon;
    @Bind(R.id.forget_phone)
    EditText mForgetPhone;
    @Bind(R.id.forget_yanzhen)
    EditText mForgetYanzhen;
    @Bind(R.id.forget_send)
    TextView mForgetSend;
    @Bind(R.id.forget_password)
    EditText mForgetPassword;
    @Bind(R.id.forget_repassword)
    EditText mForgetRepassword;
    @Bind(R.id.forget_forget)
    Button mForgetForget;
    private TimeCount time;
    private ToastUtil mToast;

    @Override
    public Integer initResid() {
        return R.layout.activity_for_get_pass_word;
    }

    @Override
    public void initCreate(Bundle savedInstanceState) {
        View view= View.inflate(this, R.layout.loading_dialog_view, null);
        mToast = new ToastUtil(this,view, Toast.LENGTH_SHORT);

        //new ProgressDialog(this).show();
        time = new TimeCount(60000, 1000,mForgetSend);
        mForgetSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String phone = mForgetPhone.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    ShowToast.showToast("手机号码不能为空");
                } else {
                    sendCode();
                    time.start();
                }
            }
        });

        mForgetBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        //更改密码
        mForgetForget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String phone = mForgetPhone.getText().toString().trim();
                String password = mForgetPassword.getText().toString().trim();
                String Repassword = mForgetRepassword.getText().toString().trim();
                String code = mForgetYanzhen.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    ShowToast.showToast("手机号码不能为空");
                } else if (TextUtils.isEmpty(password)) {
                    ShowToast.showToast("密码不能为空");
                }else if (TextUtils.isEmpty(Repassword)) {
                    ShowToast.showToast("新密码不能为空");
                } else if (TextUtils.isEmpty(code)) {
                    ShowToast.showToast("验证码码不能为空");
                } else {
                    RePasswords();
                }

            }
        });
    }
    //重置密码
    private void RePasswords() {
        MyRequest request = new MyRequest(ConStans.ForgetPassword_url, RequestMethod.POST);
        request.add("type", 2);
        request.add("phone", mForgetPhone.getText().toString().trim());
        request.add("password", mForgetPassword.getText().toString().trim());
        request.add("passwords", mForgetRepassword.getText().toString().trim());
        request.add("code", mForgetYanzhen.getText().toString().trim());
        CallServer.getInstance().addPost(this,request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {

                StatusBean statusBean = GsonUtil.GsonToBean(json, StatusBean.class);
                if (statusBean!=null) {
                    if (statusBean.status == 1) {
                        mToast.show();
                        startactivity(ForGetPassWordActivity.this, LoginActivity.class);
                        finish();
                    } else {
                        ShowToast.showToast(statusBean.msg);
                    }
                }


            }
        });
    }

    //发送验证码
    private void sendCode() {

        MyRequest request = new MyRequest(ConStans.ForgetPassword_url, RequestMethod.POST);
        request.add("type", 1);
        request.add("phone", mForgetPhone.getText().toString().trim());
        CallServer.getInstance().addPost(this,request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                StatusBean statusBean = GsonUtil.GsonToBean(json, StatusBean.class);
                if (statusBean != null) {
                    if (!TextUtils.isEmpty(statusBean.msg)) {
                        ShowToast.showToast(statusBean.msg);
                    }
                }
            }
        });
    }

}
